{
    
// 你的图片地址，可替换为自己的
const images = [
    './imgs/c1.png',
    './imgs/c2.png',
];

const carousel = document.getElementById('carousel');
const track = document.getElementById('carouselTrack');
const indicators = document.getElementById('carouselIndicators');
const slideCount = images.length;
let currentIndex = 1; // 从1开始，因前后各有一张克隆
let timer = null;
let isTransitioning = false;

// 初始化slides（首尾克隆）
function initSlides() {
    track.innerHTML = '';
    const slides = [];
    // 克隆最后一张到最前面
    slides.push(`<div class="carousel-slide"><img src="${images[slideCount - 1]}" alt=""></div>`);
    // 原始图片
    for (let img of images) {
        slides.push(`<div class="carousel-slide"><img src="${img}" alt=""></div>`);
    }
    // 克隆第一张到最后面
    slides.push(`<div class="carousel-slide"><img src="${images[0]}" alt=""></div>`);
    track.innerHTML = slides.join('');
    track.style.transform = `translateX(-${currentIndex * 100}%)`;
}

// 初始化指示点
function initIndicators() {
    indicators.innerHTML = '';
    for (let i = 0; i < slideCount; i++) {
        const btn = document.createElement('button');
        btn.className = 'carousel-indicator' + (i === 0 ? ' active' : '');
        btn.setAttribute('data-index', i);
        btn.addEventListener('click', () => {
            goToSlide(i + 1, true);
        });
        indicators.appendChild(btn);
    }
}

// 切换到指定slide
function goToSlide(index, stopAuto = false) {
    if (isTransitioning) return;
    isTransitioning = true;
    currentIndex = index;
    track.style.transition = 'transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0)';
    track.style.transform = `translateX(-${currentIndex * 100}%)`;
    updateIndicators();
    if (stopAuto) resetAutoPlay();
}

// 轮播到下一张
function nextSlide() {
    if (isTransitioning) return;
    goToSlide(currentIndex + 1);
}

// 轮播到上一张
function prevSlide() {
    if (isTransitioning) return;
    goToSlide(currentIndex - 1);
}

// 更新指示点
function updateIndicators() {
    const idx = (currentIndex - 1 + slideCount) % slideCount;
    [...indicators.children].forEach((btn, i) => {
        btn.classList.toggle('active', i === idx);
    });
}

// 处理无缝切换
track.addEventListener('transitionend', () => {
    isTransitioning = false;
    if (currentIndex === 0) {
        // 跳到倒数第二张（视觉上是最后一张）
        track.style.transition = 'none';
        currentIndex = slideCount;
        track.style.transform = `translateX(-${currentIndex * 100}%)`;
    } else if (currentIndex === slideCount + 1) {
        // 跳到第一张
        track.style.transition = 'none';
        currentIndex = 1;
        track.style.transform = `translateX(-${currentIndex * 100}%)`;
    }
});

// 自动播放
function startAutoPlay() {
    timer = setInterval(nextSlide, 2800);
}
function stopAutoPlay() {
    clearInterval(timer);
}
function resetAutoPlay() {
    stopAutoPlay();
    startAutoPlay();
}

// 鼠标悬浮/离开停止/启动自动播放
carousel.addEventListener('mouseenter', stopAutoPlay);
carousel.addEventListener('mouseleave', startAutoPlay);

// 支持触摸滑动
let startX = null, moveX = 0;
carousel.addEventListener('touchstart', e => {
    stopAutoPlay();
    startX = e.touches[0].clientX;
    moveX = 0;
    track.style.transition = 'none';
});
carousel.addEventListener('touchmove', e => {
    if (startX === null) return;
    moveX = e.touches[0].clientX - startX;
    track.style.transform = `translateX(-${currentIndex * 100 + (-moveX / carousel.offsetWidth) * 100}%)`;
});
carousel.addEventListener('touchend', () => {
    track.style.transition = 'transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0)';
    if (Math.abs(moveX) > carousel.offsetWidth / 4) {
        if (moveX < 0) nextSlide();
        else prevSlide();
    } else {
        track.style.transform = `translateX(-${currentIndex * 100}%)`;
    }
    startX = null;
    moveX = 0;
    resetAutoPlay();
});

// 初始化
initSlides();
initIndicators();
startAutoPlay();

// 窗口缩放自适应
window.addEventListener('resize', () => {
    track.style.transition = 'none';
    track.style.transform = `translateX(-${currentIndex * 100}%)`;
});

}